package com.dingding.web.wechat.action;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.dingding.common.core.util.ToolUtil;
import com.dingding.facde.activity.service.IActivityShowService;
import com.dingding.web.base.action.BaseAction;
import com.dingding.web.wechat.handler.WechatXmlHandler;

/**
 * @Description: 微信与网站交互的接口Action
 * @author fengshuonan
 * @date 2015-7-20 下午09:16:10
 * @version 微信租车V1.0
 */
@SuppressWarnings("all")
public class WechatAction extends BaseAction {
	
	private static Logger log = Logger.getLogger(WechatAction.class);
	
	@Resource(name="wechatXmlHandler")
	private WechatXmlHandler wechatXmlHandler;
	
	private String nonce;
	private String signature;
	private String timestamp;
	
	/**
	 * 
	 * @Title: receiveInteractiveXML
	 * @Description: 接收交互xml文档
	 * @author fengshuonan
	 * @return String
	 * @throws
	 */
	public String active() {

		HttpServletRequest httpServletRequest = ServletActionContext
				.getRequest();
		HttpServletResponse httpServletResponse = ServletActionContext
				.getResponse();
		
		// 输入字节流
		InputStream inputStream = null;
		// 输入字符流
		InputStreamReader input = null;
		// 字符缓冲流
		BufferedReader bufferedReader = null;

		try {
			// 获得请求字节流
			inputStream = ServletActionContext.getRequest().getInputStream();
			// 获得字符流
			input = new InputStreamReader(inputStream, "UTF-8");
			// 缓冲字符流
			bufferedReader = new BufferedReader(input);
			// 接收xml，放到StringBuffer
			StringBuffer xml = new StringBuffer();
			// 读取字符
			String line = bufferedReader.readLine();
			while (line != null) {
				xml.append(line);
				// 读取数据
				line = bufferedReader.readLine();
			}
			httpServletResponse.setCharacterEncoding("UTF-8");
			// 判断是否是微信接入激活验证，只有首次接入验证时才会收到echostr参数，此时需要把它直接返回
			String echostr = httpServletRequest.getParameter("echostr");
			if (echostr != null && echostr.length() > 1) {
				httpServletResponse.getWriter().print(echostr);
				return NONE;
			}
			
			//处理微信xml
			String responseXml = this.wechatXmlHandler.messageDispose(xml.toString());
			
			if(ToolUtil.isNotBlank(responseXml)){
				httpServletResponse.getWriter().print(responseXml);
			}

		} catch (IOException e) {
			log.error("获取输入输出流出现异常  ！ WechatAccessCheckAction : receiveInteractiveXML()", e);
		} finally {
			try {
				if (bufferedReader != null) {
					bufferedReader.close();
				}
				if (input != null) {
					input.close();
				}
				if (inputStream != null) {
					inputStream.close();
				}
			} catch (IOException e) {
				log.error("关闭流出现异常！WechatAccessCheckAction ： receiveInteractiveXML", e);
			}

		}
		return NONE;
	}

	public String getNonce() {
		return nonce;
	}

	public void setNonce(String nonce) {
		this.nonce = nonce;
	}

	public String getSignature() {
		return signature;
	}

	public void setSignature(String signature) {
		this.signature = signature;
	}

	public String getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(String timestamp) {
		this.timestamp = timestamp;
	}
	
	
	

}
